package com.sendpo.zjx.sendpo.activity;

import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sendpo.zjx.sendpo.R;
import com.sendpo.zjx.sendpo.table.MerchandiseCollection;
import com.sendpo.zjx.sendpo.table.MerchandiseHistory;
import com.sendpo.zjx.sendpo.utils.Tools;
import com.sendpo.zjx.sendpo.utils.Xutils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 朱俊鑫 on 2018
 */

public class HistoryRecordsActivity extends AppCompatActivity {

    LinearLayoutManager lm;
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_historyrecords);
        ButterKnife.bind(this);
        Tools.initStatus(this, R.color.black);
        initComponent();
    }
    private void initComponent() {
        lm=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false);
        recyclerview.setLayoutManager(lm);
        List<MerchandiseHistory> collections= Xutils.queryAll(getApplication(),MerchandiseHistory.class);
        if(collections==null||collections.size()==0){
            return;
        }
        recyclerview.setAdapter(new MyRecylcerAdaper(this,collections));
    }
    @OnClick(value = {R.id.backbt})
    public void onMyClick(View view){
        switch (view.getId()){
            case R.id.backbt:
                finish();
                break;
        }
    }

    public class MyRecylcerAdaper extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        public LayoutInflater inflater;
        public List<MerchandiseHistory> merchandiseCollections;
        public MyRecylcerAdaper(Context context, List<MerchandiseHistory> databean){
            inflater=LayoutInflater.from(context);
            this.merchandiseCollections=databean;
        }
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new IndexHoler(this.inflater.inflate(R.layout.layout_historyrecords_listitem, parent, false));
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            IndexHoler ih= (IndexHoler) holder;
            final MerchandiseHistory data =this.merchandiseCollections.get(position);
            ih.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String,Object> map=new HashMap<>();
                    map.put("merchandiseId",data.getMerchandiseId());
                    Tools.redirectActivity(HistoryRecordsActivity.this, GeneicStoreInfoActivity.class, map);
                }
            });
            ih.datetime.setText("" + Tools.getDateBeforeAfter(Long.parseLong(data.getCreatedate())));
            Glide.with(HistoryRecordsActivity.this)
                    .load(data.getMerchandiseHeadimg())
                    .asBitmap()
                    .animate(R.anim.fade_in)
                    .override(300,300)
                    .placeholder(R.color.default_lightgray)
                    .into(ih.iv);
            ih.name.setText("" + data.getMerchandiseName());
            ih.originpriceTv.setText("¥" + data.getMerchandiseOriginalprice());
            ih.currentpriceTv.setText("¥" + data.getMerchandiseCurrentprice());
            ih.text.setText(data.getMerchandiseDetails());
        }
        public void delete(int position){
            merchandiseCollections.remove(position);
            notifyDataSetChanged();
        }
        @Override
        public int getItemCount() {
            return merchandiseCollections.size();
        }
        public class IndexHoler extends RecyclerView.ViewHolder {
            @BindView(R.id.currentprice_tv)
            TextView currentpriceTv;
            @BindView(R.id.datetime)
            TextView datetime;
            @BindView(R.id.iv)
            ImageView iv;
            @BindView(R.id.name)
            TextView name;
            @BindView(R.id.originprice_tv)
            TextView originpriceTv;
            public View rootView;
            @BindView(value = R.id.text)
            public TextView text;
            public IndexHoler(View itemView) {
                super(itemView);
                rootView=itemView;
                ButterKnife.bind(this, itemView);
                this.originpriceTv.getPaint().setFlags(8);
                this.originpriceTv.getPaint().setFlags(16);
                this.originpriceTv.getPaint().setAntiAlias(true);
                this.originpriceTv.getPaint().setFlags(17);
            }
        }
    }
}
